import './style.css';
import $ from "jquery";
import AMap from 'AMap';
let div = $(
  `<div id="currentInfo">
    <div class="p p1">今天是<span class="num year"></span>年<span class="num month"></span>月<span class="num day"></span>日</div>
    <div class="p p2">星期</div>
    <div class="p p3"><span class="num time"></span></div>
    <div class="p p4"></div>
</div>`
);

// eslint-disable-next-line
Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1,
    "d+": this.getDate(),
    "h+": this.getHours(),
    "m+": this.getMinutes(),
    "s+": this.getSeconds(),
    "q+": Math.floor((this.getMonth() + 3) / 3), 
    "S": this.getMilliseconds()  
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
};
$('body').append(div);
const DAT_ARRAY = ['日', '一', '二', '三', '四', '五', '六'];
var spaceBetween = (v) => ' ' + v + ' ';
var updateCurrentInfo = function () {
  var date = new Date();
  div.find('.year').text(spaceBetween(date.getFullYear()));
  div.find('.month').text(spaceBetween(date.getMonth() + 1));
  div.find('.day').text(spaceBetween(date.getDate()));
  div.find('.p2').text('星期' + DAT_ARRAY[date.getDay()]);
  div.find('.time').text(date.Format("hh:mm"));
};
var updateTemperature = function () {
  AMap.plugin('AMap.Weather', function () {
    var weather = new AMap.Weather();
    weather.getLive(320281, function (err, data) {
      if (err) {
        div.find('.p4').text('--');
      } else {
        div.find('.p4').text(data.weather + "/" + data.temperature + '°C');
      }
    });
  });
};
updateCurrentInfo();
updateTemperature();
// 一分钟更新一次时间（会有0~59秒误差）
setInterval(updateCurrentInfo, 60000);
// 十分钟更新一次天气
setInterval(updateTemperature, 600000);
